package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.data.StudentData;
import com.entity.Student;

@WebServlet(urlPatterns = "/servlet/student.do")
public class StudentServlet extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest request
			, HttpServletResponse response) 
					throws ServletException, IOException {
		doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
	 	response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
	 	String action = (String)request.getParameter("action");
	 	
	 	PrintWriter out = response.getWriter();
	 	ServletContext application = request.getServletContext();
	 
	 	/*
	 	若action为修改或增加，做如下操作：
	 	1.创建操作对象；
	 	2.若action为修改，找到要修改的对象并赋值给操作对象；
	 	3.将操作对象和操作设置为request属性；
	 	4.请求转发到详细信息页面。
	 	*/
	 	if( "modify".equals(action) ||"add".equals(action) ){
	 		Student student = new Student();
	 		if( "modify".equals(action)){
	 			String number = (String)request.getParameter("number");
	 			List<Student> stdList = (List<Student>)application.getAttribute("stdList");
	 	 		student = StudentData.findByNumber(stdList, number);
	 		}
	 		request.setAttribute("student", student);
	 		request.setAttribute("action", action);
	 		request.getRequestDispatcher("../student/student_detail.jsp")
	 			.forward(request, response);
	 	}
	 	/*
	 	若action为保存，做如下操作：
	 	1.从表单中获取到对象数据；
	 	2.判断学号是否重复，若重复弹出提示信息，并返回；
	 	3.创建对象设置属性值；
	 	4.将对象加入到应用程序属性中；
	 	5.给出结果提示，并刷新列表页。
	 	*/
	 	else if( "save".equals(action) ){
	 		
	 		List<Student> stdList = (List<Student>)application.getAttribute("stdList");
	 		
	 		String oldNumber = request.getParameter("oldNumber");
	 		String number = request.getParameter("number");
	 		String name = request.getParameter("name");
	 		String sex = request.getParameter("sex");
	 		String[] likeArray = request.getParameterValues("like");
	 		
	 		if( StudentData.findByNumber(stdList, number) != null){
	 			out.println("<script type=\"text/javascript\">");
	 	 		out.println("alert('已有该学号的学生！');");
	 	 		out.println("window.history.go(-1);");
	 	 		out.println("</script>");
	 	 		return ;
	 		}
	 		
	 		Student student = new Student();
	 		student.setNumber(number);
	 		student.setName(name);
	 		student.setSex(sex);
	 		List<String> likeList = new ArrayList<String>();
	 		if( likeArray != null){
	 			likeList = Arrays.asList(likeArray);
	 		}
	 		student.setLike(likeList);
	 		
			
			stdList.add(student);
			application.setAttribute("stdList", stdList);
			
			out.println("<script type=\"text/javascript\">");
	 		out.println("alert('添加成功！');");
	 		out.println("window.opener.location.reload();");
	 		out.println("window.close();");
	 		out.println("</script>");

	 	}
	 	/*
	 	若action为更新，做如下操作：
	 	1.从表单中获取到对象数据；
	 	2.从对象列表中，找出要更新的对象，若找不到，则给出提示信息，并返回；
	 	3.判断学号是否重复，若重复，则给出提示信息，并返回；
	 	4.设置要更新的对象的值；
	 	5.给出结果提示，并刷新列表页。
	 	*/
	 	else if("update".equals(action)){
			List<Student> stdList = (List<Student>)application.getAttribute("stdList");
	 		
	 		String oldNumber = request.getParameter("oldNumber");
	 		String number = request.getParameter("number");
	 		String name = request.getParameter("name");
	 		String sex = request.getParameter("sex");
	 		String[] likeArray = request.getParameterValues("like");
	 		
	 		Student student = StudentData.findByNumber(stdList, oldNumber);
	 		if( student == null){
	 			out.println("<script type=\"text/javascript\">");
	 	 		out.println("alert('找不到该学生!');");
	 	 		out.println("window.history.go(-1);");
	 	 		out.println("</script>");
	 	 		return ;
	 		}
	 		else{
	 			if( !oldNumber.equals(number) 
	 					&& StudentData.findByNumber(stdList, number) != null ){
	 				out.println("<script type=\"text/javascript\">");
	 	 	 		out.println("alert('已有该学号的学生！');");
	 	 	 		out.println("window.history.go(-1);");
	 	 	 		out.println("</script>");
	 	 	 		return ;
	 			}
	 			
	 			student.setNumber(number);
	 	 		student.setName(name);
	 	 		student.setSex(sex);
	 	 		List<String> likeList = new ArrayList<String>();
	 	 		if( likeArray != null){
	 	 			likeList = Arrays.asList(likeArray);
	 	 		}
	 	 		student.setLike(likeList);
	 	 		
	 	 		out.println("<script type=\"text/javascript\">");
	 	 		out.println("alert('修改成功！');");
	 	 		out.println("window.opener.location.reload();");
	 	 		out.println("window.close();");
	 	 		out.println("</script>");
	 		}

	 	}
	 	else if("del".equals(action)){
	 		String numberStr = request.getParameter("numbers");
	 		if( numberStr != null ){
	 			String numbers[] = numberStr.split(",");
	 			if( numbers != null && numbers.length > 0){
	 	 			List<Student> stdList = (List<Student>)application.getAttribute("stdList");
	 	 			for( String number : numbers){
	 	 				for( Student std : stdList ){
	 	 					if( std.getNumber().equals(number) ){
	 	 						stdList.remove(std);
	 	 						break;
	 	 					}
	 	 				}
	 	 			}
	 	 			application.setAttribute("stdList", stdList);
	 	 			out.println("<script type=\"text/javascript\">");
	 	 	 		out.println("alert('删除完成！');");
	 	 	 		out.println("window.opener.location.reload();");
	 	 	 		out.println("window.close();");
	 	 	 		out.println("</script>");
	 	 	 		return;
	 	 		}
	 		}
	 		
			out.println("<script type=\"text/javascript\">");
	 		out.println("alert('参数错误！');");
	 		out.println("window.opener.location.reload();");
	 		out.println("window.close();");
	 		out.println("</script>");
	 		
	 	}
		
	}
	
}
